package com.example.property.adapter

import android.graphics.Color
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.example.property.R
import com.example.property.databinding.ItemCarBinding
import com.example.property.entity.CarApplyValues

class CarApplyAdapter constructor(private var list :MutableList<CarApplyValues>):RecyclerView.Adapter<CarApplyAdapter.ItemViewHolder>(){

    private var onItemClickListener:setOnItemClickListener?=null

    fun setOnItemClickListener(onItemClickListener: setOnItemClickListener){
        this.onItemClickListener = onItemClickListener
    }

    inner class ItemViewHolder(dataBinding: ItemCarBinding):RecyclerView.ViewHolder(dataBinding.root)

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ItemViewHolder {
       var dataBinding= ItemCarBinding.inflate(LayoutInflater.from(parent.context),parent,false)
        return ItemViewHolder(dataBinding)
    }

    override fun getItemCount(): Int =list.size

    override fun onBindViewHolder(holder: ItemViewHolder, position: Int) {
        var entity = list[position]
        val textView = holder.itemView.findViewById<TextView>(R.id.tv_car)
        textView.setText(entity.parkName)
        if (entity.parkId!=-1){
            textView.setBackgroundColor(Color.parseColor("#32CD32"))
            textView.setOnClickListener {
                onItemClickListener?.onItemClick(it,position)
            }
        }else{
            textView.setBackgroundColor(Color.GRAY)
        }
    }

    interface setOnItemClickListener{
        fun onItemClick(view :View, position: Int)
    }
}